package logic

import (
	"ViewSrv/global/model"
	"ViewSrv/internal/svc"
	"ViewSrv/view"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoViewLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoViewLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoViewLogic {
	return &VideoViewLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoViewLogic) VideoView(in *view.VideoViewRequest) (*view.VideoViewResponse, error) {
	// todo: add your logic here and delete this line
	var Video model.Video
	if err := model.DB.Where("id = ?", in.VideoId).Find(&Video).Error; err != nil {
		return nil, err
	}
	var TryView int64
	if Video.IsVip == 1 && in.IsVip != 1 {
		TryView = 1
	}
	var VideoLogLen model.VideoLogLen
	if err := model.DB.Where("user_id = ? and video_id = ?", in.UserId, in.VideoId).Find(&VideoLogLen).Error; err != nil {
		return nil, err
	}
	var log = model.VideoLog{
		VideoId: int(in.VideoId),
		UserId:  int(in.UserId),
	}
	if err := model.DB.Create(&log).Error; err != nil {
		return nil, err
	}
	return &view.VideoViewResponse{
		Url:     Video.Url,
		TryView: TryView,
		LostOn:  int64(VideoLogLen.Len),
	}, nil
}
